package blue.datacenter;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class MasterServer implements ServerInterface {
	String ip_;
	int port_;
	ServerSocketChannel ssock_channel_;
	
	// TODO
	MasterServer(int port) throws IOException{
		ip_ = new String("0.0.0.0");
		port_ = port;
		
		ssock_channel_ = ServerSocketChannel.open();
		System.out.println("MasterServer bind to "+ip_+":"+port_);
        InetSocketAddress inetSocketAddress = new InetSocketAddress(ip_, port_);
        ssock_channel_.socket().bind(inetSocketAddress);
        System.out.println("MasterServer success .... "+ip_+":"+port_);
	}

	public SlaveConn accept() throws IOException {
		// TODO Fix ME, current ip&port is invalid.
		// Accept client connection
        SocketChannel client_sock = ssock_channel_.accept();
        // Build a SlaveConn
        SlaveConn slave_conn = new SlaveConn(client_sock);
        
        return slave_conn;
	}
	
	public ServerSocketChannel getServerSockChannel() {return ssock_channel_;}
	
}
